﻿using XW.Utils;
//using Jint.Parser.Ast;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace XW.Core
{
    public class Where
    {
        public Where()
        {
            this.Parameters = new List<DataPrameter>();
            this.Sql = string.Empty;
        }
        public string Sql { get; set; }

        public List<DataPrameter> Parameters { get; set; }
        public Where OrWhereIf(bool isWhere, string sql, params string[] objects)  
        {
            if (!isWhere)
            {
                return this;
            }
            return OrWhere(sql, objects);
        }

        public Where AndWhereStrIf(JObject b, string key, string sqlTemplate)
        {
            string value = b.getStr(key);
            return AndWhereIf(value.IsNotNullAndEmpty(), sqlTemplate, value.SafeTrim().SafeDB());
        }

        public Where AndWhereIntIf(JObject b, string key, string sqlTemplate,Func<int?,bool> funcWhere = null) 
        {
            int? value = b.getInt(key);
            if (funcWhere == null)
            {
                funcWhere =  (value) => {
                    return value.HasValue;
                };
            }
            return AndWhereIf(funcWhere(value), sqlTemplate, (value??0).ToString());
        }


        public Where AndWhereIf(bool isWhere, string sql, params string[] objects)
        {
            if (!isWhere)
            {
                return this;
            }
            return AndWhere(sql, objects);
        }

        public Where AndWhereIf(bool isWhere, Func<string> fun)
        {
            if (!isWhere)
            {
                return this;
            }
            return AndWhere(fun);
        }

        public Where OrWhere(string sql, params string[] objects)  
        { 
            string template = this.Sql.IsNotNullOrEmpty() ? "{0} OR " : "{0}";
            this.Sql = string.Format(template, this.Sql);
            if (objects != null && objects.Length > 0)
            {
                this.Sql += string.Format(sql, objects);
            }
            else
            {
                this.Sql += sql;
            }
            return this;
        }
        public Where AndWhere(string sql, params string[] objects)
        {
            string template = this.Sql.IsNotNullOrEmpty() ? "{0} AND " : "{0} ";
            this.Sql = string.Format(template, this.Sql);
            if (objects != null && objects.Length > 0)
            {
                this.Sql += string.Format(sql, objects);
            }
            else
            {
                this.Sql += sql;
            }
            return this;
        }

        public Where AndWhere(Func<string> fun)
        {
            string template = this.Sql.IsNotNullOrEmpty() ? "{0} AND " : "{0} ";
            this.Sql = string.Format(template, this.Sql);
            this.Sql += fun();
            return this;
        }
        public override string ToString()
        {
            return this.Sql;
        }
    }

    public class DataPrameter
    {
        public string Name { get; set; }
        public Object value { get; set; }
    }
}
